package com.example.wsm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wsm.common.QueryPageParam;
import com.example.wsm.common.Result;
import com.example.wsm.entity.Menu;
import com.example.wsm.entity.User;
import com.example.wsm.service.IUserService;
import com.example.wsm.service.impl.MenuService;

import java.util.HashMap;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/hello")
    public String hello() {
        return "hello wms";
    }

    @Autowired
    private IUserService userService;
    @Autowired
    private MenuService menuService;

    // 登录
    @PostMapping("/login")
    public Result Login(@RequestBody User user) {
        List<User> list = userService.lambdaQuery().eq(User::getAccount, user.getAccount())
                .eq(User::getPassword, user.getPassword()).list();
        if (list.size() > 0) {
            User user1 = (User) list.get(0);
            List<Menu> menuList = menuService.lambdaQuery().like(Menu::getMenuright, user1.getRoleId()).list();
            HashMap<String, Object> res = new HashMap<>();
            res.put("user", user1);
            res.put("menu", menuList);
            return Result.suc(res);
        }
        return Result.fail();
    }

    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    // @PostMapping("/findById")
    // public Result findByUserid(@RequestParam String id) {
    // List list = userService.lambdaQuery().eq(User::getId, id).list();
    // return list.size() > 0 ? Result.suc(list) : Result.fail();
    // }
    @GetMapping("/findByAccount")
    public Result findByAccount(@RequestParam String account) {
        List list = userService.lambdaQuery().eq(User::getAccount, account).list();
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    // 删除
    @GetMapping("/del")
    public Result del(@RequestParam String id) {
        return userService.removeById(id) ? Result.suc() : Result.fail();
    }

    // 新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.suc() : Result.fail();
    }

    // 更新
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.suc() : Result.fail();
    }

    // // 修改
    // @PostMapping("/mod")
    // public boolean mod(@RequestBody User user) {
    // return userService.updateById(user);
    // }

    // 新增或修改
    // @PostMapping("/saveOrMod")
    // public boolean saveOrMod(@RequestBody User user) {
    // return userService.saveOrUpdate(user);
    // }

    // 查询(模糊)
    @PostMapping("/listP")
    public Result listP(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(user.getName())) {
            lambdaQueryWrapper.like(User::getName, user.getName());
        }
        return Result.suc(userService.list(lambdaQueryWrapper));
    }

    // 查询(完全)mybatis-plus lamdbaQueryWrapper
    // @PostMapping("/listW")
    // public List<User> listW(@RequestBody User user) {
    // LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    // lambdaQueryWrapper.eq(User::getName, user.getName());
    // return userService.list(lambdaQueryWrapper);
    // }

    // 分页
    // @PostMapping("/listPage")
    // // public List<User> listPage(@RequestBody HashMap map) {
    // public List<?> listPage(@RequestBody QueryPageParam query) {
    // // query.get("pageSize");
    // HashMap<?, ?> param = query.getParam();
    // String name = (String) param.get("name");
    // Page<User> page = new Page<User>();
    // page.setCurrent(query.getPageNum());
    // page.setSize(query.getPageSize());
    // LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    // lambdaQueryWrapper.like(User::getName, name);
    // IPage<User> result = userService.page(page, lambdaQueryWrapper);
    // return result.getRecords();
    // }

    @PostMapping("/listPageF")
    // public List<User> listPage(@RequestBody HashMap map) {
    public Result listPageF(@RequestBody QueryPageParam query) {
        // query.get("pageSize");
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String sex = (String) param.get("sex");
        String roleId = (String) param.get("roleId");
        Page<User> page = new Page<User>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals((name))) {
            lambdaQueryWrapper.like(User::getName, name);
        }
        if (StringUtils.isNotBlank(sex)) {
            lambdaQueryWrapper.eq(User::getSex, sex);
        }
        if (StringUtils.isNotBlank(roleId)) {
            lambdaQueryWrapper.eq(User::getRoleId, roleId);
        }
        IPage<User> result = userService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getTotal(), result.getRecords());
    }
}
